package com.plusmoney.approval.customer;


import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.plusmoney.apollo.ApolloEntrypoint;
import com.plusmoney.apollo.ConfigFileWatchDog;
import com.plusmoney.apollo.GeneralWatchDog;
import org.apache.log4j.PropertyConfigurator;

import java.util.ArrayList;
import java.util.List;

public class Main {

    private static void monitor() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                CustomerUser.shutdown();
                CustomerApproval.shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }));
    }

    public static void main(String[] args) {
        initLog();

        monitor();
	
	    CustomerUser.start();
	
	    CustomerApproval.start();
    }

    private static void initLog() {
        ApolloEntrypoint.register(buildConfigFile());
        PropertyConfigurator.configure(Main.class.getClassLoader().getResource("conf-log/log4j.properties"));
    }

    private static List<ConfigFileWatchDog> buildConfigFile() {
        List<ConfigFileWatchDog> list = new ArrayList<>();
        list.add(buildWatchDog("log4j", "conf-log/log4j.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("proxy", "proxy.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("client", "client.cnf", ConfigFileFormat.TXT));
        list.add(buildWatchDog("rocketMq", "rocketmq.properties", ConfigFileFormat.Properties));
        list.add(buildWatchDog("redis", "redis.properties", ConfigFileFormat.Properties));
        return list;
    }

    private static GeneralWatchDog buildWatchDog(String namespace, String localFile, ConfigFileFormat fileFormat) {
        return new GeneralWatchDog(namespace, Main.class.getClassLoader().getResource(localFile).getPath(), fileFormat);
    }

}
